package com.estun.backroundManagementService.modules.material.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.estun.backroundManagementService.domain.BasePageRequest;
import com.estun.backroundManagementService.domain.BaseResponse;
import com.estun.backroundManagementService.modules.material.model.MaterialPositionRelation;
import com.estun.backroundManagementService.modules.material.service.MaterialPositionRelationService;
import com.estun.backroundManagementService.utils.QueryWrapperUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/materialPosition")
@RequiredArgsConstructor
public class MaterialPositionController {

    private final MaterialPositionRelationService materialPositionRelationService;


    //分页查询工位物料绑定信息
    @PostMapping("/page")
    public BaseResponse page(@RequestBody BasePageRequest<MaterialPositionRelation> pageRequest) {
        Page<MaterialPositionRelation> page = new Page<>(pageRequest.getPage(), pageRequest.getSize());
        QueryWrapper<MaterialPositionRelation> queryWrapper = QueryWrapperUtil.buildQueryWrapper(pageRequest.getQuery());
        Page<MaterialPositionRelation> page1 = materialPositionRelationService.page(page, queryWrapper);
        return BaseResponse.success(page1);
    }

    // 根据ID获取工位物料绑定信息
    @GetMapping("/{id}")
    public BaseResponse getMaterialById(@PathVariable String id) {
        return BaseResponse.success(materialPositionRelationService.getById(id));
    }

    // 添加工位物料绑定信息
    @PostMapping("/add")
    public BaseResponse addMaterial(@RequestBody MaterialPositionRelation material) {
        return BaseResponse.success(materialPositionRelationService.save(material));
    }

    // 更新工位物料绑定信息
    @PutMapping("/update")
    public BaseResponse updateMaterial(@RequestBody MaterialPositionRelation material) {
        return BaseResponse.success(materialPositionRelationService.updateById(material));
    }

    // 删除工位物料绑定信息
    @DeleteMapping("/delete/{id}")
    public BaseResponse deleteMaterial(@PathVariable String id) {
        return BaseResponse.success(materialPositionRelationService.removeById(id));
    }
}
